package com.rjwh.dingdong.client.activity.drawbook;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.RelativeLayout;

import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.BaseActivity;
import com.rjwh.dingdong.client.activity.webbrowser.OaGameActivity;
import com.rjwh.dingdong.client.adapter.DrawBookMyCatalogGvAdapter;
import com.rjwh.dingdong.client.application.MyApplication;
import com.rjwh.dingdong.client.bean.localbean.LocalGmaeInfo;
import com.rjwh.dingdong.client.constant.LocalConstant;
import com.rjwh.dingdong.client.service.HttpWebCoreService;
import com.rjwh.dingdong.client.util.FileUtils;
import com.rjwh.dingdong.client.util.ToastUtil;

import java.io.File;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author Kay
 * @ClassName DrawBookMyCatalogActivity
 * @Description 我的绘本(已下载的)
 * @date 2016-8-8
 * @history 2016-8-8 author: description:
 */
public class DrawBookMyCatalogActivity extends BaseActivity {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.app_bar_layout)
    AppBarLayout appBarLayout;
    @BindView(R.id.draw_book_my_catalog_gv)
    GridView drawBookMyCatalogGv;
    @BindView(R.id.draw_book_my_catalog_empty_root_fl)
    RelativeLayout drawBookMyCatalogEmptyRootFl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.draw_book_my_catalog_layout);
        ButterKnife.bind(this);
        initView();
    }

    private void initView() {
        initToolbarHelper();
        List<LocalGmaeInfo> listData = MyApplication.db.findAllByWhere(LocalGmaeInfo.class, "type=1");
        if (listData == null || listData.isEmpty()) {
            drawBookMyCatalogEmptyRootFl.setVisibility(View.VISIBLE);
        } else {
            drawBookMyCatalogEmptyRootFl.setVisibility(View.GONE);
            DrawBookMyCatalogGvAdapter adapter = new DrawBookMyCatalogGvAdapter(this);
            drawBookMyCatalogGv.setAdapter(adapter);
            adapter.appendToList(listData);
            drawBookMyCatalogGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    LocalGmaeInfo item = (LocalGmaeInfo) parent.getAdapter().getItem(position);
                    int pid = item.getPid();
                    File file = new File(FileUtils.getDrwabookDirDirectory(), pid + "/");
                    if (file.isDirectory() && file.listFiles().length > 0) {
                        String userid = MyApplication.spUtil.getStrPreferenceByParamName(MyApplication.app, LocalConstant.SP_USERID);
                        String babyid = MyApplication.spUtil.getStrPreferenceByParamName(MyApplication.app, LocalConstant.SP_BABYID);
                        String token = MyApplication.spUtil.getStrPreferenceByParamName(MyApplication.app, LocalConstant.SP_USER_TOKEN);
                        String uuid = MyApplication.deviceId;
                        String version = MyApplication.versionCode;
                        String url = String.format(HttpWebCoreService.HUIBEN_URL, pid, userid,
                                babyid, version, uuid, token, System.currentTimeMillis(), pid);
                        OaGameActivity.startActivity(DrawBookMyCatalogActivity.this, url);
                    } else {
                        ToastUtil.showToast(DrawBookMyCatalogActivity.this, "路径错误");
                    }
                }
            });
        }
    }

    /**
     * init the toolbar
     */
    protected void initToolbarHelper() {
        this.setSupportActionBar(this.toolbar);
        getSupportActionBar().setTitle("我的绘本");
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }

    @OnClick(R.id.draw_book_my_catalog_empty_btn)
    public void onClick() {
        Intent intent = new Intent(this, DrawBookCatalogMainActivity.class);
        startActivity(intent);
        finish();
    }
}
